layui.use(['form', 'table', 'upload'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        upload = layui.upload;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){

        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/web/boArticle/getInfoById";
            $.get(url, {id: id}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    var isPassSelect = $("select[name='isPass'] option");
                    for(var i = 0; i < isPassSelect.length; i++){
                        if($(isPassSelect[i]).val() == data.isPass){
                            $(isPassSelect[i]).attr("selected", "selected");
                            break;
                        }
                    }
                }else{
                    layer.msg(data.message, {icon:2});
                }
                form.render();
            })
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var index = layer.load(2);
        var url = "/web/boArticle/updatePass";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });

    //点击取消
    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

});